package com.quan.commons.base;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quan.commons.utils.PageBean;

import tk.mybatis.mapper.entity.Condition;

public class BaseService<T> implements IBaseService<T> {

	@Autowired
	private BaseMapper<T> mapper;

	@Override
	public T insert(T entity) {
		this.mapper.insert(entity);
		return entity;
	}

	@Override
	public T insertSelective(T entity) {
		this.mapper.insertSelective(entity);
		return entity;
	}
	
	@Override
	public int update(T entity) {
		return this.mapper.updateByPrimaryKey(entity);
	}

	@Override
	public int update(T entity, Condition condition) {
		return this.mapper.updateByCondition(entity, condition) ;
	}
	
	@Override
	public int updateSelective(T entity) {
		return this.mapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int updateSelective(T entity, Condition condition) {
		return this.mapper.updateByConditionSelective(entity, condition) ;
	}
	
	@Override
	public int delete(T entity) {
		return this.mapper.delete(entity);
	}

	@Override
	public int delete(Condition condition) {
		return this.mapper.deleteByCondition(condition) ;
	}

	@Override
	public int deleteById(Object id) {
		return this.mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteByIds(String ids) {
		return this.mapper.deleteByIds(ids);
	}

	@Override
	public T selectOneById(Object id) {
		return this.mapper.selectByPrimaryKey(id);
	}
	
	@Override
	public T selectOne(T entity) {
		return this.mapper.selectOne(entity);
	}

	@Override
	public List<T> selectAll(T entity) {
		return this.mapper.select(entity);
	}

	@Override
	public List<T> selectAllByCondition(Condition condition) {
		return this.mapper.selectByCondition(condition) ;
	}
	
	@Override
	public int selectCount(T entity) {
		return this.mapper.selectCount(entity);
	}
	
	@Override
	public int selectCountByCondition(Condition condition) {
		return this.mapper.selectCountByCondition(condition) ;
	}

	@Override
	public int existValue(Class<?> clazz, String property, Object value) {
		Condition condition = new Condition(clazz) ;
		condition.createCriteria().andEqualTo(property, value) ;
		return this.mapper.selectCountByCondition(condition) ;
	}
	
	@Override
	public PageInfo<T> selectPage(T entity, PageBean pageBean) {
		Page<T> page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());

		this.mapper.selectPage(entity, pageBean) ;

		return new PageInfo<T>(page);
	}

	@Override
	public PageInfo<T> selectPageByMap(Map<String, Object> params, PageBean pageBean) {
		Page<T> page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());

		this.mapper.selectPageByMap(params, pageBean) ;

		return new PageInfo<T>(page);
	}
	
	@Override
	public PageInfo<T> selectPageByCondition(Condition condition, PageBean pageBean) {
		Page<T> page = PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());

		this.mapper.selectByCondition(condition);

		return new PageInfo<T>(page);
	}

	
	/*@Override
	public PageInfo<T> selectPage(T entity, PageBean pageBean) {
		Page<T> page = PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
		Condition condition = new Condition(entity.getClass());
		// 条件
		Criteria criteria = condition.createCriteria();
		criteria.andEqualTo(entity);
		// 排序
		if (null != pageBean && null != pageBean.getOrderBy()) {
			condition.setOrderByClause(pageBean.getOrderBy());
		}
		this.mapper.selectByCondition(condition);
		return new PageInfo<T>(page);
	}*/

}
